var util = require('../../utils/util.js');
// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    remark:'',
    phone:0,
    roleid:0,
    cartList:[],
    sumMoney:0,
    orderNumber:0,
    signature:'',
    list:[],
    userInfo:[],
    username:'',
    time:'2021-12-8',
    lists:[],
  },
  beizhu:function(e){
    this.setData({
      remark:e.detail.value
    })
  },

  //提交表单
listGet:function(){
  var that = this;
  var arr = new Array;
  var i =0;

  for(var index in that.data.list){  //循环遍历提交订单
    var obj = {
      list_detail:[]
    }
    arr.push(obj);
  }
  for(var index in that.data.list){
    var obj={
      username: that.data.username,
      goods_id: that.data.list[index].id,
      num: that.data.list[index].num,
      price_out: that.data.list[index].price,
      money: that.data.list[index].money,
      
    }
    arr[index].list_detail.push(obj)
    
  }
  console.log(arr)
  that.setData({
    lists:arr
  })
  wx.setStorageSync('buy_list',that.data.lists);
  
  //提交订单数据

  wx.request({
    url: "http://localhost:8080/wechat/buy_do",
    method: "POST",
    data: {
      buy_list_list: that.data.lists,
      userInfo: "hello"
    },
    header: {
      "content-type": "application/JSON",
    },
    success: function (res) {
      console.log(that.data.lists);
      if (res.statusCode == 200) {
        if(res.data != null){
          wx.showToast({
            title: '支付成功',
            icon: 'true',
            duration: 2500
          })
          wx.removeStorageSync('cartList');
          wx.removeStorageSync('sumMoney');
          wx.removeStorageSync('orderNumber');
          wx.removeStorageSync('list');
          wx.switchTab({
            url: '/pages/test-main/test-main',  //支付成功
          })
        // var json=JSON.stringify(that.data.list);
        // console.log(json)
        // console.log(that.data.remark)
      }else{
        wx.showToast({
          title: '支付失败',
          icon: 'fail1',
          duration: 1000
        })
        console.info(res.statusCode+1)
        console.log(res.errMsg)
        console.log(res.data)
      }
      }else {
        // 返回list
        // wx.reLaunch({
        //   url: '/pages/list/list',
        // })
        console.info(res.statusCode+2)
        console.log(res.errMsg)
        console.log(res.data)
      }
    }
  })

},

  payforit:function(){
    var that = this;
    wx.request({
      url: 'http://host:8080/buy_do', // 支付订单，修改订单状态，修改数据库
      method: 'post',
      data: {
        //传递state参数，遍历本次支付
        mobile:that.data.phone,
        userID:that.data.roleid,
        remark:that.data.remark,
        content:JSON.stringify(that.data.cartList),
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': that.data.token
      },
      success(res) {
        wx.hideLoading();
        console.info(res.data);
        if (res.data['msg'] == "成功") {
          wx.redirectTo({
            url: '../home/home'
          })
        } else {
          wx.showToast({
            title: res.data['msg'],
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    var userInfo =wx.getStorageSync('userInfo');
    var signature = wx.getStorageSync('openID');
    var list = wx.getStorageSync('list');
    var username = wx.getStorageSync('account');
    console.info("openid");
    console.info(wx.getStorageSync('openID'));
    this.setData({
      list: list,
      cartList: wx.getStorageSync('cartList'),
      sumMoney: wx.getStorageSync('sumMoney'),
      orderNumber: wx.getStorageSync('orderNumber'),
      userInfo: userInfo,
      signature: signature,
      username: userInfo.nickName,
      // time: TIME,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})